﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class GiftExchange
    {
        public int Add(Chain.Model.GiftExchange model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into GiftExchange(");
            builder.Append("MemID,ExchangeTelePhone,ExchangeAddress,ExchangeAccount,ExchangeAllNumber,ExchangeAllPoint,ApplicationTime,ApplicationRemark,ExchangeStatus,ExchangeType,ShopID)");
            builder.Append(" values (");
            builder.Append("@MemID,@ExchangeTelePhone,@ExchangeAddress,@ExchangeAccount,@ExchangeAllNumber,@ExchangeAllPoint,@ApplicationTime,@ApplicationRemark,@ExchangeStatus,@ExchangeType,@ShopID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemID", SqlDbType.Int, 4), new SqlParameter("@ExchangeTelePhone", SqlDbType.VarChar, 50), new SqlParameter("@ExchangeAddress", SqlDbType.NVarChar, 50), new SqlParameter("@ExchangeAccount", SqlDbType.VarChar, 50), new SqlParameter("@ExchangeAllNumber", SqlDbType.Int, 4), new SqlParameter("@ExchangeAllPoint", SqlDbType.Int, 4), new SqlParameter("@ApplicationTime", SqlDbType.DateTime), new SqlParameter("@ApplicationRemark", SqlDbType.NVarChar, 500), new SqlParameter("@ExchangeStatus", SqlDbType.Int, 4), new SqlParameter("@ExchangeType", SqlDbType.Int, 4), new SqlParameter("@ShopID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.MemID;
            cmdParms[1].Value = model.ExchangeTelePhone;
            cmdParms[2].Value = model.ExchangeAddress;
            cmdParms[3].Value = model.ExchangeAccount;
            cmdParms[4].Value = model.ExchangeAllNumber;
            cmdParms[5].Value = model.ExchangeAllPoint;
            cmdParms[6].Value = model.ApplicationTime;
            cmdParms[7].Value = model.ApplicationRemark;
            cmdParms[8].Value = model.ExchangeStatus;
            cmdParms[9].Value = model.ExchangeType;
            cmdParms[10].Value = model.ShopID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataTable AgainPrint(int rechargeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            builder.Append("MemCard,MemName,ExchangeAllPoint,ExchangeAllNumber,MemPoint,UserName,ExchangeTime,ExchangeAccount ");
            builder.Append("from SysUser,GiftExchange,Mem ");
            builder.Append("where SysUser.UserID=GiftExchange.ExchangeUserID ");
            builder.Append("and Mem.MemID=GiftExchange.MemID ");
            builder.Append("and ExchangeID=@ExchangeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ExchangeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = rechargeID;
            return DbHelperSQL.Query(builder.ToString(), cmdParms).Tables[0];
        }

        public bool Delete(int ExchangeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GiftExchange ");
            builder.Append(" where ExchangeID=@ExchangeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ExchangeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ExchangeID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string ExchangeIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GiftExchange ");
            builder.Append(" where ExchangeID in (" + ExchangeIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int ExchangeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from GiftExchange");
            builder.Append(" where ExchangeID=@ExchangeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ExchangeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ExchangeID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataTable GetExchangeIDByGiftNameOrGiftCode(string giftNameOrGiftCodeStrWhere)
        {
            return DbHelperSQL.Query("select DISTINCT ExchangeID from GiftExchangeDetail join PointGift on GiftExchangeDetail.ExchangeGiftID=PointGift.GiftID where " + giftNameOrGiftCodeStrWhere).Tables[0];
        }

        public DataSet GetGiftExchange(string strSql)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select isnull(sum(ExchangeAllNumber),0) as ExchangeAllNumber ,isnull(sum(ExchangeAllPoint),0) as ExchangeAllPoint from GiftExchange,Mem ");
            if (strSql != "")
            {
                builder.Append(" where " + strSql);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetGiftExchangeByTime(DateTime starttime, DateTime endtime, string strwhere)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@StartTime", SqlDbType.DateTime), new SqlParameter("@EndTime", SqlDbType.DateTime), new SqlParameter("@strWhere", SqlDbType.NVarChar, 0x7d0) };
            parameters[0].Value = starttime;
            parameters[1].Value = endtime;
            parameters[2].Value = strwhere;
            return DbHelperSQL.RunProcedure("MonthGiftExchange", parameters, "#GiftExchangeData");
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ExchangeID,MemID,ExchangeTelePhone,ExchangeAddress,ExchangeAccount,ExchangeAllNumber,ExchangeAllPoint,ApplicationTime,ApplicationRemark,ExchangeStatus,ExchangeTime,ExchangeUserID,ExchangeRemark,ExchangeType ");
            builder.Append(" FROM GiftExchange ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ExchangeID,MemID,ExchangeTelePhone,ExchangeAddress,ExchangeAccount,ExchangeAllNumber,ExchangeAllPoint,ApplicationTime,ApplicationRemark,ExchangeStatus,ExchangeTime,ExchangeUserID,ExchangeRemark,ExchangeType ");
            builder.Append(" FROM GiftExchange ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ExchangeID desc");
            }
            builder.Append(")AS Row, T.*  from GiftExchange T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListGiftExchangeSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "GiftExchange";
            string[] column = new string[] { "ExchangeID, ExchangeAccount,ExchangeAllNumber,ExchangeAllPoint,ApplicationTime,ExchangeStatus,ExchangeRemark,ExchangeType" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ExchangeID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetListGiftExchangeSPForWeiXin(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "GiftExchange,Mem";
            string[] column = new string[] { "ExchangeID, ExchangeAccount,ExchangeAllNumber,ExchangeAllPoint,ApplicationTime,ExchangeStatus,ExchangeRemark,ExchangeType" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ExchangeID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "GiftExchange,Mem,SysShop,SysUser";
            string[] column = new string[] { "ExchangeAccount,MemCard,MemName,ExchangeAllNumber,ExchangeAllPoint,ApplicationTime,ExchangeTime,ShopName,UserName,ExchangeType,ExchangeID,ExchangeStatus" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ExchangeID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ExchangeID", "GiftExchange");
        }

        public Chain.Model.GiftExchange GetModel(int ExchangeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ExchangeID,MemID,ExchangeTelePhone,ExchangeAddress,ExchangeAccount,ExchangeAllNumber,ExchangeAllPoint,ApplicationTime,ApplicationRemark,ExchangeStatus,ExchangeTime,ExchangeUserID,ExchangeRemark,ExchangeType from GiftExchange ");
            builder.Append(" where ExchangeID=@ExchangeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ExchangeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ExchangeID;
            Chain.Model.GiftExchange exchange = new Chain.Model.GiftExchange();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["ExchangeID"] != null) && (set.Tables[0].Rows[0]["ExchangeID"].ToString() != ""))
            {
                exchange.ExchangeID = int.Parse(set.Tables[0].Rows[0]["ExchangeID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MemID"] != null) && (set.Tables[0].Rows[0]["MemID"].ToString() != ""))
            {
                exchange.MemID = int.Parse(set.Tables[0].Rows[0]["MemID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeTelePhone"] != null) && (set.Tables[0].Rows[0]["ExchangeTelePhone"].ToString() != ""))
            {
                exchange.ExchangeTelePhone = set.Tables[0].Rows[0]["ExchangeTelePhone"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ExchangeAddress"] != null) && (set.Tables[0].Rows[0]["ExchangeAddress"].ToString() != ""))
            {
                exchange.ExchangeAddress = set.Tables[0].Rows[0]["ExchangeAddress"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ExchangeAccount"] != null) && (set.Tables[0].Rows[0]["ExchangeAccount"].ToString() != ""))
            {
                exchange.ExchangeAccount = set.Tables[0].Rows[0]["ExchangeAccount"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ExchangeAllNumber"] != null) && (set.Tables[0].Rows[0]["ExchangeAllNumber"].ToString() != ""))
            {
                exchange.ExchangeAllNumber = int.Parse(set.Tables[0].Rows[0]["ExchangeAllNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeAllPoint"] != null) && (set.Tables[0].Rows[0]["ExchangeAllPoint"].ToString() != ""))
            {
                exchange.ExchangeAllPoint = int.Parse(set.Tables[0].Rows[0]["ExchangeAllPoint"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ApplicationTime"] != null) && (set.Tables[0].Rows[0]["ApplicationTime"].ToString() != ""))
            {
                exchange.ApplicationTime = DateTime.Parse(set.Tables[0].Rows[0]["ApplicationTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ApplicationRemark"] != null) && (set.Tables[0].Rows[0]["ApplicationRemark"].ToString() != ""))
            {
                exchange.ApplicationRemark = set.Tables[0].Rows[0]["ApplicationRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ExchangeStatus"] != null) && (set.Tables[0].Rows[0]["ExchangeStatus"].ToString() != ""))
            {
                exchange.ExchangeStatus = int.Parse(set.Tables[0].Rows[0]["ExchangeStatus"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeTime"] != null) && (set.Tables[0].Rows[0]["ExchangeTime"].ToString() != ""))
            {
                exchange.ExchangeTime = DateTime.Parse(set.Tables[0].Rows[0]["ExchangeTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeUserID"] != null) && (set.Tables[0].Rows[0]["ExchangeUserID"].ToString() != ""))
            {
                exchange.ExchangeUserID = int.Parse(set.Tables[0].Rows[0]["ExchangeUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ExchangeRemark"] != null) && (set.Tables[0].Rows[0]["ExchangeRemark"].ToString() != ""))
            {
                exchange.ExchangeRemark = set.Tables[0].Rows[0]["ExchangeRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ExchangeType"] != null) && (set.Tables[0].Rows[0]["ExchangeType"].ToString() != ""))
            {
                exchange.ExchangeType = int.Parse(set.Tables[0].Rows[0]["ExchangeType"].ToString());
            }
            return exchange;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM GiftExchange ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataSet GetVerifyListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "GiftExchange,Mem";
            string[] column = new string[] { "ExchangeID, ExchangeTelePhone, ExchangeAddress, ExchangeAccount, ExchangeAllNumber, ExchangeAllPoint, ApplicationTime, ApplicationRemark, ExchangeStatus, ExchangeTime, ExchangeUserID, ExchangeRemark, ExchangeType, ShopID,Mem.* " };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ExchangeID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int MainSystemAdd(Chain.Model.GiftExchange model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into GiftExchange(");
            builder.Append("MemID,ExchangeTelePhone,ExchangeAddress,ExchangeAccount,ExchangeAllNumber,ExchangeAllPoint,ApplicationTime,ApplicationRemark,ExchangeStatus,ExchangeTime,ExchangeUserID,ExchangeRemark,ExchangeType,ShopID)");
            builder.Append(" values (");
            builder.Append("@MemID,@ExchangeTelePhone,@ExchangeAddress,@ExchangeAccount,@ExchangeAllNumber,@ExchangeAllPoint,@ApplicationTime,@ApplicationRemark,@ExchangeStatus,@ExchangeTime,@ExchangeUserID,@ExchangeRemark,@ExchangeType,@ShopID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemID", SqlDbType.Int, 4), new SqlParameter("@ExchangeTelePhone", SqlDbType.VarChar, 50), new SqlParameter("@ExchangeAddress", SqlDbType.NVarChar, 50), new SqlParameter("@ExchangeAccount", SqlDbType.VarChar, 50), new SqlParameter("@ExchangeAllNumber", SqlDbType.Int, 4), new SqlParameter("@ExchangeAllPoint", SqlDbType.Int, 4), new SqlParameter("@ApplicationTime", SqlDbType.DateTime), new SqlParameter("@ApplicationRemark", SqlDbType.NVarChar, 500), new SqlParameter("@ExchangeStatus", SqlDbType.Int, 4), new SqlParameter("@ExchangeTime", SqlDbType.DateTime), new SqlParameter("@ExchangeUserID", SqlDbType.Int, 4), new SqlParameter("@ExchangeRemark", SqlDbType.NVarChar, 500), new SqlParameter("@ExchangeType", SqlDbType.Int, 4), new SqlParameter("@ShopID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.MemID;
            cmdParms[1].Value = model.ExchangeTelePhone;
            cmdParms[2].Value = model.ExchangeAddress;
            cmdParms[3].Value = model.ExchangeAccount;
            cmdParms[4].Value = model.ExchangeAllNumber;
            cmdParms[5].Value = model.ExchangeAllPoint;
            cmdParms[6].Value = model.ApplicationTime;
            cmdParms[7].Value = model.ApplicationRemark;
            cmdParms[8].Value = model.ExchangeStatus;
            cmdParms[9].Value = model.ExchangeTime;
            cmdParms[10].Value = model.ExchangeUserID;
            cmdParms[11].Value = model.ExchangeRemark;
            cmdParms[12].Value = model.ExchangeType;
            cmdParms[13].Value = model.ShopID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.GiftExchange model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update GiftExchange set ");
            builder.Append("MemID=@MemID,");
            builder.Append("ExchangeTelePhone=@ExchangeTelePhone,");
            builder.Append("ExchangeAddress=@ExchangeAddress,");
            builder.Append("ExchangeAccount=@ExchangeAccount,");
            builder.Append("ExchangeAllNumber=@ExchangeAllNumber,");
            builder.Append("ExchangeAllPoint=@ExchangeAllPoint,");
            builder.Append("ApplicationTime=@ApplicationTime,");
            builder.Append("ApplicationRemark=@ApplicationRemark,");
            builder.Append("ExchangeStatus=@ExchangeStatus,");
            builder.Append("ExchangeTime=@ExchangeTime,");
            builder.Append("ExchangeUserID=@ExchangeUserID,");
            builder.Append("ExchangeRemark=@ExchangeRemark,");
            builder.Append("ExchangeType=@ExchangeType");
            builder.Append(" where ExchangeID=@ExchangeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemID", SqlDbType.Int, 4), new SqlParameter("@ExchangeTelePhone", SqlDbType.VarChar, 50), new SqlParameter("@ExchangeAddress", SqlDbType.NVarChar, 50), new SqlParameter("@ExchangeAccount", SqlDbType.VarChar, 50), new SqlParameter("@ExchangeAllNumber", SqlDbType.Int, 4), new SqlParameter("@ExchangeAllPoint", SqlDbType.Int, 4), new SqlParameter("@ApplicationTime", SqlDbType.DateTime), new SqlParameter("@ApplicationRemark", SqlDbType.NVarChar, 500), new SqlParameter("@ExchangeStatus", SqlDbType.Int, 4), new SqlParameter("@ExchangeTime", SqlDbType.DateTime), new SqlParameter("@ExchangeUserID", SqlDbType.Int, 4), new SqlParameter("@ExchangeRemark", SqlDbType.NVarChar, 500), new SqlParameter("@ExchangeType", SqlDbType.Int, 4), new SqlParameter("@ExchangeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.MemID;
            cmdParms[1].Value = model.ExchangeTelePhone;
            cmdParms[2].Value = model.ExchangeAddress;
            cmdParms[3].Value = model.ExchangeAccount;
            cmdParms[4].Value = model.ExchangeAllNumber;
            cmdParms[5].Value = model.ExchangeAllPoint;
            cmdParms[6].Value = model.ApplicationTime;
            cmdParms[7].Value = model.ApplicationRemark;
            cmdParms[8].Value = model.ExchangeStatus;
            cmdParms[9].Value = model.ExchangeTime;
            cmdParms[10].Value = model.ExchangeUserID;
            cmdParms[11].Value = model.ExchangeRemark;
            cmdParms[12].Value = model.ExchangeType;
            cmdParms[13].Value = model.ExchangeID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

